home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Periodicals / develop / develop 6 code / TCP / finger / TCPHi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-03  |  6.8 KB  |  267 lines  |  [TEXT/MPS ]

  1. /*----------------------------------------------------------
  2. #
  3. #    finger -- MPW Tool
  4. #
  5. #    Written by Steven Falkenburg
  6. #
  7. #-----------------------------------------------------------
  8. #
  9. #    TCPHi.c
  10. #
  11. #    This module contains high-level routines for setting
  12. #    up connections using MacTCP and sending/receiving data
  13. #    on these connections.
  14. #
  15. #-----------------------------------------------------------*/
  16.  
  17. #pragma segment lowlevel
  18.  
  19. #include "compat.h"
  20. #include <string.h>
  21.  
  22. #ifdef PROTOS
  23. #include <Types.h>
  24. #include <Memory.h>
  25. #include <Dialogs.h>
  26. #include <Resources.h>
  27. #include <CursorCtl.h>
  28. #include <Strings.h>
  29. #include <Lists.h>
  30. #endif
  31.  
  32. #include "MacTCPCommonTypes.h"
  33. #include "GetMyIPAddr.h"
  34. #include "TCPPB.h"
  35. #include "TCPRoutines.h"
  36. #include "TCPHi.h"
  37.  
  38.  
  39. /* InitNetwork opens the network driver
  40. */
  41.  
  42. OSErr InitNetwork(void)
  43. {
  44.     return OpenTCPDriver();
  45. }
  46.  
  47.  
  48. /* CreateStream() creates an unconnected network stream to be
  49.    used later by OpenConnection.  The length of the receive
  50.    buffer must be specified in the call */
  51.    
  52. OSErr CreateStream(unsigned long *stream,unsigned long recvLen)
  53. {
  54.     Ptr recvPtr;
  55.     OSErr err;
  56.     
  57.     recvPtr = NewPtr(recvLen);
  58.     err = MemError();
  59.     if (err==noErr)
  60.         err = LowTCPCreateStream(stream,recvPtr,recvLen,(TCPNotifyProc)nil);
  61.     return err;
  62. }
  63.  
  64.  
  65. /* OpenConnection() initiates a connection to a remote machine,
  66.    given that machine's network number and connection port.  A timeout
  67.    value for the call must be given, and the stream identifier is returned. */
  68.  
  69. OSErr OpenConnection(unsigned long stream,long remoteHost,short remotePort,Byte timeout)
  70. {
  71.     ip_addr localHost;
  72.     tcp_port localPort = 0;
  73.     
  74.     return LowTCPOpenConnection(stream,timeout,remoteHost,remotePort,&localHost,
  75.                                 &localPort);
  76. }
  77.  
  78.  
  79. /* WaitForConnection() listens for a connection on a particular port from a
  80.     particular host.  It returns when a connection has been established */
  81.  
  82. OSErr WaitForConnection(unsigned long stream,Byte timeout,short localPort,
  83.                         long *remoteHost,short *remotePort)
  84. {
  85.     ip_addr localHost;
  86.     
  87.     return LowTCPWaitForConnection(stream,timeout,(ip_addr *)remoteHost,
  88.                 (tcp_port *)remotePort,&localHost,(tcp_port *)&localPort,false,nil);
  89. }
  90.  
  91.  
  92. /* AsyncWaitForConnection() listens for a connection on a particular port from a
  93.     particular host.  It is executed asynchronously and returns immediately */
  94.  
  95. void AsyncWaitForConnection(unsigned long stream,Byte timeout,short localPort,
  96.                 long remoteHost,short remotePort,TCPiopb **returnBlock)
  97. {
  98.     ip_addr localHost;
  99.     
  100.     LowTCPWaitForConnection(stream,timeout,(ip_addr *)&remoteHost,
  101.                 (tcp_port *)&remotePort,&localHost,(tcp_port *)&localPort,true,returnBlock);
  102. }
  103.  
  104.  
  105. /* AsyncGetConnectionData() should be called when a call to AsyncWaitForConnection
  106.     completes (when returnBlock->ioResult <= 0).  This call retrieves the information
  107.     about this new connection and disposes the parameter block. */
  108.     
  109. OSErr AsyncGetConnectionData(TCPiopb *returnBlock,long *remoteHost,short *remotePort)
  110. {
  111.     ip_addr localHost;
  112.     tcp_port localPort;
  113.     
  114.     return LowFinishTCPWaitForConn(returnBlock,(ip_addr *)remoteHost,
  115.                         (tcp_port *)remotePort,&localHost,&localPort);
  116. }
  117.  
  118.  
  119. /* CloseConnection() terminates a connection to a remote host, given the
  120.    stream identifier of the connection */
  121.    
  122. OSErr CloseConnection(unsigned long stream)
  123. {
  124.     return LowTCPClose(stream,10);
  125. }
  126.  
  127.  
  128. /* AbortConnection() aborts a connection to a remote host, given the
  129.    stream identifier of the connection */
  130.    
  131. OSErr AbortConnection(unsigned long stream)
  132. {
  133.     return LowTCPAbort(stream);
  134. }
  135.     
  136.  
  137. /* ReleaseStream() frees the allocated buffer space for a given connection
  138.    stream.  This call should be made after CloseConnection. */
  139.    
  140. OSErr ReleaseStream(unsigned long stream)
  141. {
  142.     OSErr err;
  143.     Ptr recvPtr;
  144.     unsigned long recvLen;
  145.     
  146.     if ((err = LowTCPRelease(stream,&recvPtr,&recvLen)) == noErr)
  147.             DisposPtr(recvPtr);
  148.     
  149.     return err;
  150. }
  151.  
  152.  
  153. /* SendData() sends data along a connection stream to a remote host. */
  154.  
  155. OSErr SendData(unsigned long stream,Ptr data,unsigned short length,Boolean retry)
  156. {    
  157.     OSErr err;
  158.     struct wdsEntry myWDS[2];    /* global write data structure */
  159.  
  160.     myWDS[0].length = length;
  161.     myWDS[0].ptr = data;
  162.     myWDS[1].length = 0;
  163.     myWDS[1].ptr = nil;
  164.     do
  165.         err = LowTCPSendData(stream,20,false,false,(Ptr) myWDS,false,nil);
  166.     while (retry && err==commandTimeout);
  167.     return err;
  168. }
  169.  
  170. /* SendMultiData() is similar to SendData, but takes an array of strings to send
  171.    to the remote host. */
  172.  
  173. OSErr SendMultiData(unsigned long stream,Str255 data[],short numData,Boolean retry)
  174. {
  175.     struct wdsEntry *theWDS;
  176.     short i;
  177.     OSErr err;
  178.     
  179.     theWDS = (wdsEntry *)NewPtr((numData+1) * sizeof(wdsEntry));
  180.     if (MemError())
  181.         return MemError();    
  182.     theWDS[numData].length = 0;
  183.     theWDS[numData].ptr = nil;
  184.     for (i=0; i<numData; i++) {
  185.         theWDS[i].ptr = data[i];
  186.         theWDS[i].length = strlen((char *)data[i]);
  187.     }
  188.     do
  189.         err = LowTCPSendData(stream,20,false,false,(Ptr) theWDS,false,nil);
  190.     while (retry && err==commandTimeout);
  191.     DisposPtr((Ptr)theWDS);
  192.     return err;
  193. }
  194.  
  195.  
  196. /* SendDataAsync() sends data to a remote host asynchronously.  The ioResult
  197.    parameter block variable should be checked, and SendDataDone() called when
  198.    this flag is zero or negative */
  199.  
  200. void SendDataAsync(unsigned long stream, Ptr data,unsigned short length,TCPiopb **returnBlock)
  201. {    
  202.     struct wdsEntry *theWDS;
  203.  
  204.     InitNetCursor();
  205.     theWDS = (wdsEntry *)NewPtr( (2*sizeof(wdsEntry)) );
  206.     theWDS[0].length = length;
  207.     theWDS[0].ptr = data;
  208.     theWDS[1].length = 0;
  209.     theWDS[1].ptr = 0;
  210.     LowTCPSendData(stream,20,false,false,(Ptr) theWDS,true,returnBlock);
  211.     TrashNetCursor();
  212. }
  213.  
  214.  
  215. /* SendDataDone() should be called in response to the completion of a SendDataAsync
  216.    call.  It returns any error which occurred in the send. */
  217.  
  218. OSErr SendAsyncDone(TCPiopb *returnBlock)
  219. {
  220.     DisposPtr((Ptr)returnBlock->csParam.send.wdsPtr);
  221.     return LowFinishTCPSend(returnBlock);
  222. }
  223.  
  224.  
  225. /* RecvData() waits for data to be received on a connection stream.  When data
  226.    arrives, it is copied into the data buffer and the call terminates. */
  227.  
  228. OSErr RecvData(unsigned long stream,Ptr data,unsigned short *length,Boolean retry)
  229. {
  230.     Boolean    urgent,mark;
  231.     OSErr    err;
  232.     unsigned short recvLength;
  233.  
  234.     do {
  235.         recvLength = *length;
  236.         err = LowTCPRecvData(stream,20,&urgent,&mark,data,&recvLength,false,nil);
  237.     }
  238.     while (retry && err==commandTimeout);
  239.     *length = recvLength;
  240.     if (err == noErr) {
  241.         *(data+*length) = '\0';
  242.     }
  243.     return err;
  244. }
  245.  
  246.  
  247. /* RecvDataAsync() is identical to RecvData above, but in this case, the call is
  248.    made asynchronously. */
  249.  
  250. void RecvDataAsync(unsigned long stream,Ptr data,unsigned short length,TCPiopb **returnBlock)
  251. {
  252.     Boolean urgent,mark;
  253.     
  254.     LowTCPRecvData(stream,20,&urgent,&mark,data,&length,true,returnBlock);
  255. }
  256.  
  257.  
  258. /* GetDataLength() should be called in response to the completion of the
  259.    RecvDataAsync call. */
  260.  
  261. OSErr GetDataLength(TCPiopb *returnBlock,unsigned short *length)
  262. {
  263.     Boolean urgent,mark;
  264.     
  265.     return LowFinishTCPRecv(returnBlock,&urgent,&mark,length);
  266. }
  267.